package Day16;

import java.util.*;

public class Main {

    public static HashMap<String, Integer> pokers;

    public static void iniPokers () {
        pokers = new HashMap<>();
        pokers.put("3", 1);
        pokers.put("4", 2);
        pokers.put("5", 3);
        pokers.put("6", 4);
        pokers.put("7", 5);
        pokers.put("8", 6);
        pokers.put("9", 7);
        pokers.put("10", 8);
        pokers.put("J", 9);
        pokers.put("Q", 10);
        pokers.put("K", 11);
        pokers.put("A", 12);
        pokers.put("2", 13);
        pokers.put("joker", 14);
        pokers.put("JOKER", 15);

    }

    public static void main(String[] args) {

        iniPokers();

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {

            String s = scanner.nextLine();
            String[] ss = s.split("-");
            String[] s1 = ss[0].split(" ");
            String[] s2 = ss[1].split(" ");

            int cond1 = getCondition(s1);
            int cond2 = getCondition(s2);

            //记录胜负情况
            int choice = 0;
            //有错牌直接输出
            if (cond1 == -1 || cond2 == -1 || s1.length == 0 || s2.length == 0) {
                System.out.print("ERROR");
                return;
            }
            //数量相同比大小
            else if (cond1 == cond2) {
                if (pokers.getOrDefault(s1[0], -1) < pokers.getOrDefault(s2[0], -1)) {
                    choice = 2;
                } else  {
                    choice = 1;
                }
            }
            //有王炸直接输出
            else if (cond1 == 6 || cond2 == 6) {
                if (cond1 == 6) {
                    choice = 1;
                }
                if (cond2 == 6) {
                    choice = 2;
                }
            }

            if (choice == 1) {
                int i = 0;
                for (i = 0; i < s1.length - 1; i++) {
                    System.out.print(s1[i] + " ");
                }
                System.out.print(s1[i]);
            } else if (choice == 2) {
                int i = 0;
                for (i = 0; i < s2.length - 1; i++) {
                    System.out.print(s2[i] + " ");
                }
                System.out.print(s2[i]);
            } else {
                System.out.print("ERROR");
            }
        }

    }

    public static int getCondition (String[] s) {

        //非法字符直接-1
        for (String value : s) {
            if (!pokers.containsKey(value)) {
                return -1;
            }
        }

        //判断个子
        if (s.length == 1) {
            return 1;
        }
        //判断对子和王炸
        else if (s.length == 2) {
            if (Objects.equals(s[0], s[1])) {
                return 2;
            } else if (Objects.equals(s[0], "joker") || Objects.equals(s[0], "JOKER")) {
                if (Objects.equals(s[1], "joker") || Objects.equals(s[1], "JOKER")) {
                    return 6;
                }
            } else {
                return -1;
            }
        }
        //判断三个
        else if (s.length == 3) {
            for (int i = 1; i < 3; i++) {
                if (!Objects.equals(s[i], s[0])) {
                    return -1;
                }
            }
            return 3;
        }
        //判断炸弹
        else if (s.length == 4) {
            for (int i = 1; i < 4; i++) {
                if (!Objects.equals(s[i], s[0])) {
                    return -1;
                }
            }
            return 4;
        }
        //判断顺子
        else if (s.length == 5) {

            boolean isTrue = true;
            if (pokers.get(s[0]) > pokers.get(s[1])) {
                for (int i = 1; i < 4; i++) {
                    if (pokers.get(s[i]) < pokers.get(s[i + 1])) {
                        isTrue = false;
                    }
                }
            } else if (pokers.get(s[0]) < pokers.get(s[1])) {
                for (int i = 1; i < 4; i++) {
                    if (pokers.get(s[i]) > pokers.get(s[i + 1])) {
                        isTrue = false;
                    }
                }
            }
            if (isTrue) {
                return 5;
            } else {
                return -1;
            }
        }

        return -1;
    }

    public static void main0(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        int sum = 0;
        for (int i = 1; i < n + 1; i++) {
            if (isPerfectNum(i)) {
                sum++;
                System.out.println(i);
            }
        }
        System.out.println(sum);

    }

    public static boolean isPerfectNum (int n) {
        List<Integer> list = getFactors(n);
        int sum = 0;
        for (Integer integer : list) {
            sum += integer;
        }
        return sum == n;
    }

    public static List<Integer> getFactors (int n) {
        List<Integer> list = new ArrayList<>();
        int factor = n;
        list.add(1);
        for (int i = 2; i < factor; i++) {
            if (n % i == 0) {
                list.add(i);
                list.add(n / i);
                factor = n / i;
            }
        }

        return list;
    }

}